package com.zd.novel.controller;

import com.zd.common.bean.ZdNovelResponse;
import com.zd.common.pojo.Chapter;
import com.zd.common.pojo.Novel;
import com.zd.common.pojo.NovelChapter;
import com.zd.common.pojo.Classify;
import com.zd.novel.service.NovelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import  com.zd.common.enums.ResponseStatus;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 小说微服务Controller
 */
@RestController
@RequestMapping("/novel")
public class NovelController {
    @Autowired
    private NovelService novelService;

    /**
     * TODO
     * 小说分类列表查询接口
     */
    @GetMapping("/listNovelClassify")
    public ZdNovelResponse listNovelCategory(){
        return ZdNovelResponse.ok(new ArrayList<Classify>());
    }

    /**
     * TODO
     * 小说详情信息查询接口
     */
    @GetMapping("/{id}")
    public ZdNovelResponse<Novel> queryNovelDetail(@PathVariable("id") Long novelId){
        return ZdNovelResponse.ok(new Novel());
    }

    /**
     * TODO
     * 小说章节列表查询接口
     * */
    @GetMapping("/listNovelChapter")
    public ZdNovelResponse<List<NovelChapter>> listNewIndex(@RequestParam("novelId") Long novelId){
        return null;
    }

    /**
     * TODO
     * 小说章节查询接口
     * */
    @GetMapping("/queryNovelChapter")
    public ZdNovelResponse<NovelChapter> queryNovelChapter(@RequestParam("chapterId") Long chapterId){
        return null;
    }

    /**
     * TODO
     * 上一章节和下一章节目录ID查询接口
     * */
    @GetMapping("/queryNearbyChapterId")
    public ZdNovelResponse<Map<String,Long>> queryNearbyChapterId(
            @RequestParam("novelId") Long novelId,
            @RequestParam("chapterNum") Integer chapterNum){
        /*使用map保存上一章节id和下一章节id*/
        return null;
    }

    /**
     * TODO
     * 小说内容查询接口
     * */
    @GetMapping("/queryNovelContent")
    public ZdNovelResponse<Classify> queryNovelContent(@RequestParam("chapterId") Long chapterId){
        return null;
    }

    /**
     * TODO
     * 同类推荐书籍查询接口
     * */
    @GetMapping("/listNovelByCatId")
    public ZdNovelResponse<List<Novel>> listNovelByCatId(@RequestParam("catId") Integer catId){
        return null;
    }


    /**
     *管理员新建小说接口
     * 前端传入.小说名,作家id,分类id
     * 随即判断小说名是否合法/null,通过则执行下一步
     * 业务层查询书名/
     */
    //      /novel
    @PostMapping("/insertNovel/{id}")
    public ZdNovelResponse insertNovel(@RequestBody Novel novel,
                                       @PathVariable Integer id) {
        String msg="";
        try {

//            System.out.println(id);
//            System.out.println(novel);

            String name = novel.getNovelName();
            if ("".equals(name) || name.length() == 0) {
                msg="小说名不能为空";
                return ZdNovelResponse.fail(ResponseStatus.PARAM_ERROR, msg);
            }
            msg = novelService.insertNovel(novel, id);
            /**
             * 操作数据库返回情况
             * msg="你不是作家,请先申请成为作家";
             *msg="书名重复,请更改您的书名";
             * msg="没有该用户,请确认该用户是否存在!";
             */
//            if ("没有该用户,请确认该用户是否存在!".equals(msg)) {
//                return ZdNovelResponse.fail(ResponseStatus.PARAM_ERROR, msg);
//
//            } else if ("你不是作家,请先申请成为作家".equals(msg)) {
//                return ZdNovelResponse.fail(ResponseStatus.PARAM_ERROR, msg);
//            } else if ("书名重复,请更改您的书名".equals(msg)) {
//                return ZdNovelResponse.fail(ResponseStatus.PARAM_ERROR, msg);
//            }

            return ZdNovelResponse.ok(msg);


        } catch (Exception e) {
            msg="网络出现异常,请稍后再试";
            e.printStackTrace();

        }
        return ZdNovelResponse.fail(ResponseStatus.PARAM_ERROR, msg);

    }
    /**
     * 新增章节:前提需要:新建书籍.
     * 前端反后端:章节名:chapterName,章节内容:context,小说ID:novelID
     * 该层做出判断:判断章节内容是否大于或等于1000字.是则允许写入且进入审核状态,否则直接返回
     *
     * Chapter
     */
    @PostMapping("/insertChapter/{novelId}")
    public ZdNovelResponse insertChapter(@RequestBody Chapter chapter,@PathVariable Integer novelId){
        String msg="";
        /**
         * 根据前端传回来的内容进行判断,标题名不能为空,内容必须大于等于1000字
         */
        String chapterName = chapter.getChapterName();
        //System.out.println(chapterName.length());
        String context = chapter.getContext();
        //System.out.println(context);
        System.out.println("该段内容的总字数为:"+context.length());
        if ("".equals(chapterName) && chapterName.length()==0){
            return ZdNovelResponse.fail(ResponseStatus.PARAM_ERROR,"章节名不能为空,请重新输入!");
        }
        if (context.length()>=300){
            System.out.println("当前字数为:"+context.length()+",可以新增");
            msg=novelService.insertChapert(chapter,novelId);

            return null;

        }
        System.out.println("当前字数:"+ context.length()+",不予审核");
        return  ZdNovelResponse.fail(ResponseStatus.PARAM_ERROR,"内容字符不足1000,无法进行审核,请继续编写");
    }






}
